package com.example.itshome.network;

import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import com.example.itshome.entity.ActivityEntity;
import com.example.itshome.manager.UserInfoManager;
import org.json.JSONException;
import org.json.JSONObject;

public class SignUpActivityRequest extends AbstractNetworkRequest{

    private int activityId;
    private Button signUpButton;

    public SignUpActivityRequest setActivityId(int activityId) {
        this.activityId = activityId;
        return this;
    }

    public SignUpActivityRequest setSignUpButton(Button signUpButton) {
        this.signUpButton = signUpButton;
        return this;
    }

    public SignUpActivityRequest(Activity activity) {
        super(activity);
    }

    @Override
    protected String getPathAndParams() {
        return "activity/sign-up?username="+ UserInfoManager.getInstance().getUser().getUsername()+
                "&activityId="+activityId;
    }

    @Override
    protected void handleResult(String result) {
        try {
            JSONObject jsonObject=new JSONObject(result);
            int code= jsonObject.getInt("code");
            if(code!=200){
                return;
            }
            setHandleResultRunnable(new Runnable() {
                @Override
                public void run() {
                    signUpButton.setText("已报名");
                    signUpButton.setClickable(false);
                    activity.setResult(1,new Intent().putExtra("id",activityId));
                }
            });
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
}
